var express = require('express');
var router = express.Router();
// const Blog = require("../beans/blog")
const indexController = require("../controllers/index_controller")
/* GET home page. */
// 获取首页信息
router.get('/indexfo', function (req, res, next) {
  indexController.doGetAllIndexInfo((result) => {
    res.json(result)
  })

});
// 获取商品详情
// detail/${id}
router.get('/detail/:goodsID', function (req, res, next) {
  var {
    goodsID
  } = req.params
  // console.log(goodsID);
  indexController.doGetgoodsDetail((result) => {
    // console.log("laji",result);
    var detailList = []
    if (result.code == 200) {
      result.data.forEach(item => {

        for (var key in item.data.data) {
          item.data.data[key].forEach(ite => {
            if (ite.goodsId == goodsID * 1) {
              console.log(ite);
              detailList.push(ite)
            }

          })
        }
      })
      res.json({
        code: 200,
        data: detailList,
        mag: "获取数据成功"
      })

    } else {
      res.json({
        code: 401,
        data: {},
        mag: "获取数据失败"
      })
    }
  })
})
//后台添加轮播图Rotation
// Rotation为对象，轮播图信息
// var Rotation={
//   RotationId:1,
//   name:"http//www.baidu.com",
//   msg:"百度"
// }

router.post("/Rotation", (req, res) => {
  var {
    Rotation
  } = req.body
  indexController.AddRotationlist(Rotation, (result) => {
    res.json(result)
  })
})
//后台修改轮播图
// var Rotation = {
//   "RotationId": 1,
//   "name": "http//www.charge.com",
//   "msg": "谷歌"
// }

router.put("/Rotation", (req, res) => {
  var {
    Rotation
  } = req.body
  indexController.putRotationlist(Rotation, (result) => {
    res.json(result)
  })
})
//后台删除轮播图
// 轮播图图片id
// var RotationId = [1]

router.delete("/Rotation", (req, res) => {
  var {
    RotationId
  } = req.body
  indexController.deleteRotationlist(RotationId, (result) => {
    res.json(result)
  })
})
//热卖商品添加
// var goods={
//   goodsId:1,
//   name:"maojin",
//   price:100
// }
router.post("/hotgoods", (req, res) => {
  var {
    goods
  } = req.body
  indexController.AddGoodslist(goods, (result) => {
    res.json(result)
  })
})
//热卖商品修改
// var putgoods = {
//   goodsId:1,
//   name:"laji",
//   price:100
// }
router.put("/hotgoods", (req, res) => {
  var {
    putgoods
  } = req.body
  indexController.putGoodslist(putgoods, (result) => {
    res.json(result)
  })
})
//热卖商品删除
// var goodsId = [1]

router.delete("/hotgoods", (req, res) => {
  var {
    goodsId
  } = req.body
  indexController.deletegoodslist(goodsId, (result) => {
    res.json(result)
  })
})
//新品上线添加
// var newgoods={
//   goodsId:1,
//   name:"maojin",
//   price:100
// }
router.post("/newgoods", (req, res) => {
  var {
    newgoods
  } = req.body
  indexController.AddNewGoodslist(newgoods, (result) => {
    res.json(result)
  })
})
//新品修改
// var putnewgoods = {
//   goodsId:1,
//   name:"feiwu",
//   price:100
// }
router.put("/newgoods", (req, res) => {
  var {
    putnewgoods
  } = req.body
  indexController.putnewGoodslist(putnewgoods, (result) => {
    res.json(result)
  })
})
//新品删除
// var newgoodsId = [1]

router.delete("/newgoods", (req, res) => {
  var {
    newgoodsId
  } = req.body
  indexController.deletenewgoodslist(newgoodsId, (result) => {
    res.json(result)
  })
})
// 商品推荐添加
// var remgoods={
//   goodsId:1,
//   name:"maojin",
//   price:100
// }
router.post("/remgoods", (req, res) => {
  var {
    remgoods
  } = req.body
  indexController.AddRemGoodslist(remgoods, (result) => {
    res.json(result)
  })
})
//商品推荐修改
// var putRemgoods = {
//   goodsId:1,
//   name:"feiwu1",
//   price:100
// }
router.put("/remgoods", (req, res) => {
  var {
    putRemgoods
  } = req.body
  indexController.putRemGoodslist(putRemgoods, (result) => {
    res.json(result)
  })
})
//商品推荐删除
var remgoodsId = [1]

router.delete("/remgoods", (req, res) => {
  var {
    // remgoodsId
  } = req.body
  indexController.deleteRemgoodslist(remgoodsId, (result) => {
    res.json(result)
  })
})
module.exports = router;